package com.sleepingcat.ft_flutter.activity

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentActivity
import com.cat.android.lib_base.base.RouterActivity
import com.idlefish.flutterboost.FlutterBoost
import com.idlefish.flutterboost.containers.FlutterActivityLaunchConfigs
import com.idlefish.flutterboost.containers.FlutterBoostFragment
import com.sleepingcat.ft_flutter.fragment.MyFlutterFragment
import com.sleepingcat.lib_flutter.R
import io.flutter.embedding.android.FlutterFragment
import io.flutter.embedding.android.FlutterFragmentActivity

/**
 * @作者 志浩
 * @时间 2025/2/28 22:01
 * @描述 TODO
 */
class MyFlutterActivity : FlutterFragmentActivity() {
    private val TAG = "FlutterActivity"

    companion object {
        fun startActivity(context: Context, uniqueId: String?, url: String?, urlParams: Map<String, Any>? = emptyMap()) {
            val intent = Intent(context, MyFlutterActivity::class.java)
            val hashMap = hashMapOf<String, Any>()
            urlParams?.let {
                hashMap.putAll(it)
            }
            intent
                .putExtra("uniqueId", uniqueId)
                .putExtra("url", url)
                .putExtra("urlParams", hashMap)
                .putExtra("opaque", true)
            FlutterBoost.instance().currentActivity().startActivity(intent)
        }

        fun startActivityForResult(context: Context, uniqueId: String?, url: String?, urlParams: Map<String, Any>? = emptyMap(), launcher: ActivityResultLauncher<Intent>) {
            val intent = Intent(context, MyFlutterActivity::class.java)
            val hashMap = hashMapOf<String, Any>()
            urlParams?.let {
                hashMap.putAll(it)
            }
            intent
                .putExtra("uniqueId", uniqueId)
                .putExtra("url", url)
                .putExtra("urlParams", hashMap)
                .putExtra("opaque", true)
            launcher.launch(intent)
        }
    }

    private var flutterFragment: FlutterBoostFragment? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun createFlutterFragment(): FlutterFragment {
        val uniqueId = intent.getStringExtra("uniqueId")
        val url = intent.getStringExtra("url")
        val urlParams = intent.getSerializableExtra("urlParams") as Map<String, Any>
        flutterFragment = MyFlutterFragment.build(uniqueId, url, urlParams, true)
        return flutterFragment!!
    }

    override fun onBackPressed() {
        if (flutterFragment != null) {
            flutterFragment!!.onBackPressed()
        } else {
            super.onBackPressed()
        }
    }
}